package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.shape.layout.ShapeFrameLayout;
import com.qiantu.api.BuildConfig;
import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.utils.ThemeUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.glide.GlideApp;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.qiantu.phone.ui.uiutil.LLSceneLinkageUtil;

import java.util.List;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 状态数据列表
 */
public final class LoadDeviceAdapter extends AppAdapter<DeviceBean> {

    private boolean selectable = false;

    public LoadDeviceAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {
        private final ShapeFrameLayout deviceBackView;
        private final ImageView iconDevice;
        private final TextView tvDeviceName;
        private final TextView tvFloorRoom;
        private final ImageView tvDeviceFlag;
        private final TextView tvOffline;
        private final TextView tvStatus;
        private final ImageView btnDeviceOperation;

        private ViewHolder() {
            super(R.layout.item_load_device);
            deviceBackView = findViewById(R.id.view_device_back);
            iconDevice = findViewById(R.id.icon_device);
            tvDeviceName = findViewById(R.id.tv_device_name);
            tvFloorRoom = findViewById(R.id.tv_floor_room);
            tvDeviceFlag = findViewById(R.id.tv_device_flag);
            tvOffline = findViewById(R.id.tv_offline);
            tvStatus = findViewById(R.id.tv_status);
            btnDeviceOperation = findViewById(R.id.btn_device_operate);
        }

        @Override
        public void onBindView(int position) {
            RecyclerView.LayoutParams rl = (RecyclerView.LayoutParams) itemView.getLayoutParams();
            if(position == getItemCount() - 1 || (getItemCount() % 2 == 0 && position == getItemCount() - 2)){
                rl.bottomMargin = getResources().getDimensionPixelSize(R.dimen.dp_12);
            }else{
                rl.topMargin = 0;
                rl.bottomMargin = 0;
            }
            itemView.setLayoutParams(rl);
            DeviceBean item = getItem(position);
            String imageUrl;
//            switch (ThemeUtil.themeId){
//                case ThemeUtil.THEME_ORANGE:
                    imageUrl = BuildConfig.APP_RESOURCE_BASE + ThemeUtil.getIconThemeSuffix() + item.getImageUrl() + "_on_home.png";
//                    break;
//                default:
//                    imageUrl = item.getOnImageUrl();
//                    break;
//            }
            GlideApp.with(iconDevice).load(imageUrl).into(iconDevice);
//            GlideApp.with(iconDevice).load(item.getStatusImageUrl()).into(iconDevice);
            tvDeviceName.setText(item.getName());
            if(item.getLinkType() == 3){
                tvFloorRoom.setText(item.getFloorName());
            }else{
                tvFloorRoom.setText(LLRoomDBManager.getInstance(getContext()).getFloorRoomName(item.getRoomSerialNo()));
            }
            if(selectable){
                btnDeviceOperation.setImageResource(item.getIsDisable() ? R.mipmap.icon_list_choose_not : R.mipmap.icon_list_choose_multiple);
            }else{
                if(item.getIsOpen()){
                    deviceBackView.getShapeDrawableBuilder().setSolidColor(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
                    tvDeviceName.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.textColor4));
                    tvFloorRoom.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.textColor4));
                }else{
                    deviceBackView.getShapeDrawableBuilder().setSolidColor(AppApplication.getInstance().getThemeColor(R.attr.deviceSolidBgColor));
                    tvDeviceName.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.textColor1));
                    tvFloorRoom.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.textColor2));
                }
            }
            deviceBackView.getShapeDrawableBuilder().intoBackground();
            if(item.getIsOnline()){
                tvOffline.setVisibility(View.GONE);
            }else{
                tvOffline.setVisibility(View.VISIBLE);
            }

            tvDeviceFlag.setVisibility(View.GONE);
            if(item.getIsDisable()){
//                tvDeviceFlag.setEnabled(false);
//                tvDeviceFlag.setVisibility(View.VISIBLE);
//                tvDeviceFlag.setImageDrawable(AppApplication.getInstance().getDrawable(R.mipmap.icon_device_unable));
            }else{
                if(item.getIsFavourite()){
                    tvDeviceFlag.setEnabled(true);
                    tvDeviceFlag.setVisibility(View.VISIBLE);
                    tvDeviceFlag.setImageDrawable(AppApplication.getInstance().getDrawable(R.mipmap.icon_device_favorite));
                }
            }
            if(item.getIsEnvironment() == 0){
//                tvStatus.setVisibility(View.GONE);
                tvStatus.setText(LLDeviceUtil.getDeviceValue(item));
            }else{
                tvStatus.setVisibility(View.VISIBLE);
                tvStatus.setText(LLDeviceUtil.getEnviromentData(item) + LLDeviceUtil.getEnviromentUnit(item));
            }
        }
    }

    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }

    public void update(DeviceBean newDevice) {
        for (int i = 0; i < getData().size(); i++) {
            DeviceBean device = getData().get(i);
            //设备号相同并且deviceType相同才进入
            if (!TextUtils.isEmpty(device.getDeviceSerialNo())
                    && device.getDeviceSerialNo().equals(newDevice.getDeviceSerialNo())) {
                //在线状态不一样或者deviceType或者设备首页属性不一致才刷新
                    getData().set(i, newDevice);
                    notifyItemChanged(i);
                break;
            }
        }
    }

    public void update(List<DeviceBean> devices) {
        for (int i = 0; i < getData().size(); i++) {
            DeviceBean device = getData().get(i);
            for(int j = 0; j<devices.size(); j++){
                if (!TextUtils.isEmpty(device.getDeviceSerialNo())
                        && device.getDeviceSerialNo().equals(devices.get(j).getDeviceSerialNo())) {
                    if(device.getIsOnline() != devices.get(j).getIsOnline()
                            || !device.getImageUrl().equals(devices.get(j).getImageUrl())
                            || !device.getName().equals(devices.get(j).getName())
                            || !device.getRoomSerialNo().equals(devices.get(j).getRoomSerialNo())
                            || !device.getDeviceType().equals(devices.get(j).getDeviceType())
                            || !device.getStateData().getStatus().homeDeviceEquals(devices.get(j).getStateData().getStatus())){
                        getData().set(i, devices.get(j));
                    }
                    break;
                }
            }
        }
    }

    public void setSelectable(boolean selectable){
        this.selectable = selectable;
    }
}